﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FacebookAccessLayer;
using DatabaseLayer;

namespace IVRJunctionUI
{
    public partial class FacebookAccessToken : System.Web.UI.Page
    {
        String code = null;
        protected void Page_Load(object sender, EventArgs e)
        {
         
                code = Request.QueryString["code"];

                if (code != null)
                {
                    FacebookFunctions objectFacebookFunctions = new FacebookFunctions();
                    String accessToken = objectFacebookFunctions.getAccessTokenFromCode(code);

                    if (accessToken == null)
                        LabelStatus.Text = "Error getting access token";
                    else
                    {
                        bool uploadSuccess = objectFacebookFunctions.uploadTestStatus(accessToken);
                        LabelStatus.Text = Convert.ToString(uploadSuccess);

                        if (uploadSuccess)
                        {
                            DatabaseFunctions objectDatabaseFunctions = new DatabaseFunctions();
                            LabelStatus.Text = Convert.ToString(objectDatabaseFunctions.insertOrUpdateFacebookToken(accessToken));

                        }
                    }
                }

                else
                    LabelStatus.Text = "Please avoid refereshing the page";
            
        }
    }
}